package com.example.aevovein.data;

import android.content.Context;
import android.widget.Toast;
import android.util.Log;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.example.aevovein.model.Skill;

public class SkilltreeInitializer implements Initializer {
    private static final String TAG = "SkilltreeInitializer";
    private final Gson gson;

    public SkilltreeInitializer() {
        this.gson = new GsonBuilder().setPrettyPrinting().create();
    }

    @Override
    public void initialize(Context context, File dataDir) {
        File skilltreeFile = new File(dataDir, "skilltree.json");
        try {
            if (!skilltreeFile.exists()) {
                if (!skilltreeFile.createNewFile()) {
                    Log.e(TAG, "Failed to create skilltree.json");
                    Toast.makeText(context, "创建技能数据文件失败", Toast.LENGTH_SHORT).show();
                    return;
                }
                try (FileWriter writer = new FileWriter(skilltreeFile)) {
                    // 创建一个多维结构的技能数据
                    Map<String, List<Skill>> skills = new HashMap<>();
    
                    // 练字技能列表
                    List<Skill> verticalSkills = createVerticalSkills();
                    
                    // 申论技能列表
                    List<Skill> shilunSkills = createShilunSkills();
                    
                    // 面试技能列表
                    List<Skill> interviewSkills = createInterviewSkills();
                    
                    // 行测技能列表
                    List<Skill> xingceSkills = createXingceSkills();

                    // 将四个技能列表分别加入到技能树中
                    skills.put("练字", verticalSkills);
                    skills.put("申论", shilunSkills);
                    skills.put("面试", interviewSkills);
                    skills.put("行测", xingceSkills);

                    // 使用Gson将Map转换为JSON并写入文件
                    gson.toJson(skills, writer);
                    Log.d(TAG, "Successfully created initial skill data");
                }
            }
        } catch (IOException e) {
            Log.e(TAG, "Error creating skill data: " + e.getMessage(), e);
            Toast.makeText(context, "创建技能数据失败: " + e.getMessage(), Toast.LENGTH_SHORT).show();
        }
    }

    private List<Skill> createVerticalSkills() {
        List<Skill> verticalSkills = new ArrayList<>();
        
        addSkill(verticalSkills, "skill.vertical_basic", "基础笔画", "练习基本笔画");
        addSkill(verticalSkills, "skill.vertical_structure", "结构练习", "练习字的结构");
        addSkill(verticalSkills, "skill.vertical_character", "单字练习", "练习单个字");
        addSkill(verticalSkills, "skill.vertical_paragraph", "段落练习", "练习段落");
        
        return verticalSkills;
    }

    private List<Skill> createShilunSkills() {
        List<Skill> shilunSkills = new ArrayList<>();
        
        addSkill(shilunSkills, "skill.shilun_basic", "基础理论", "学习申论基础理论");
        addSkill(shilunSkills, "skill.shilun_analysis", "材料分析", "练习材料分析");
        addSkill(shilunSkills, "skill.shilun_writing", "写作练习", "练习申论写作");
        addSkill(shilunSkills, "skill.shilun_practice", "真题练习", "练习申论真题");
        
        return shilunSkills;
    }

    private List<Skill> createInterviewSkills() {
        List<Skill> interviewSkills = new ArrayList<>();
        
        addSkill(interviewSkills, "skill.interview_basic", "基础理论", "学习面试基础理论");
        addSkill(interviewSkills, "skill.interview_practice", "模拟练习", "进行面试模拟练习");
        addSkill(interviewSkills, "skill.interview_expression", "表达能力", "提升表达能力");
        addSkill(interviewSkills, "skill.interview_confidence", "自信心", "提升面试自信心");
        
        return interviewSkills;
    }

    private List<Skill> createXingceSkills() {
        List<Skill> xingceSkills = new ArrayList<>();
        
        addSkill(xingceSkills, "skill.xingce_language", "言语理解", "练习言语理解");
        addSkill(xingceSkills, "skill.xingce_judgment", "判断推理", "练习判断推理");
        addSkill(xingceSkills, "skill.xingce_quantity", "数量关系", "练习数量关系");
        addSkill(xingceSkills, "skill.xingce_data", "资料分析", "练习资料分析");
        addSkill(xingceSkills, "skill.xingce_common", "常识判断", "练习常识判断");
        
        return xingceSkills;
    }

    private void addSkill(List<Skill> skills, String id, String name, String description) {
        Skill skill = new Skill();
        skill.setId(id);
        skill.setName(name);
        skill.setDescription(description);
        skill.setLevel(0);
        skill.setMaxLevel(100);
        skills.add(skill);
    }
} 